package com.rural.bus.driver.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.rural.bus.driver.MyApplication;
import com.rural.bus.driver.OrderDealActivity;
import com.rural.bus.driver.R;
import com.rural.bus.driver.adapter.NewOrderAdapter;
import com.rural.bus.driver.model.Order;
import com.rural.bus.driver.view.xlist.XListView;

public class NewOrderFragment extends BaseFragment implements OnItemClickListener {

	private XListView listview;
	private List<Order> mOrders = new ArrayList<Order>();
	private NewOrderAdapter mAdapter;
	private String userId;

	SoundPool sp;
	HashMap<Integer, Integer> spMap;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new NewOrderAdapter(getActivity(), mOrders, listview);
		listview.setAdapter(mAdapter);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_new_order, container, false);
		initViewWithData(view);
		initSound();
		return view;
	}

	private void initViewWithData(View view) {
		userId = MyApplication.driverInfo.drivId;

		listview = (XListView) view.findViewById(R.id.listview);
		listview.setPullRefreshEnable(false);
		listview.setPullLoadEnable(false);
		listview.setOnItemClickListener(this);

	}

	private void initSound() {
		sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
		spMap = new HashMap<Integer, Integer>();
		spMap.put(1, sp.load(getActivity(), R.raw.sound_new_order, 1));
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	public void addOrderItem(Order order) {
		mOrders.add(0, order);
		playSound(1, 0);// 0:表示不重复
		mAdapter.notifyDataSetChanged();
	}

	public void removeOrderItemById(int orderId) {
		for (Order order : mOrders) {
			if (order.orderId == orderId) {
				mOrders.remove(order);
				break;
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position > 0 && position <= mOrders.size()) {
			Intent intent = new Intent(getActivity(), OrderDealActivity.class);
			intent.putExtra("order", mOrders.get(position - 1));
			startActivity(intent);
		}

	}

	public void playSound(int sound, int number) {
		AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
		float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		float volumnRatio = volumnCurrent / audioMaxVolumn;
		sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1f);
	}

}
